/*kvan
 *2014.5.12
 */
package com.taxsystem.likaifan.taxcompute;

import java.util.concurrent.ExecutionException;

import org.json.JSONException;
import org.json.JSONObject;

import com.main.taxsystem.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SecondHandHouseCompute extends Activity {

	public final static String RECI_COAST = "sw.system.HTTUTIL";

	private String result =
		"{"
		+ "\"message\": \"OK\","
		+ "\"serverTime\": \"2014-05-27 15:28:44\","
		+ "\"data\": {\"deedTax\": 0,\"salseTax\": 82500,\"personalTax\": 100000,\"landValueIncrementTax\": 15000},\"code\": 0}";

	

	final int MY_NOTICE = 0x111;
	final int NAV_SETTING = 0x112;
	final int ACCOUNT_SETTING = 0x113;
	final int INDIVIDUAL_SETTING = 0x114;
	private EditText old_total_price;
	private EditText total_price;
	private EditText house_area;;
	private Spinner house_type;
	private Spinner calc_type;

	private CheckBox over_5_years;
	private CheckBox seller_only;
	private CheckBox buyer_only;
	
	private static String[] house_type_str = {"普通住房","非普通住房","经济适用房"};
	private int house_type_result;
	
	private static String[] calc_type_str = {"卖方","买方"};
	private int calc_type_result;
	
	private Button button;
	
	private String requestUrl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second_hand_house_compute);

		// 注册广播
		total_price = (EditText) findViewById(R.id.total_price);
		old_total_price = (EditText) findViewById(R.id.old_total_price);
		house_area = (EditText) findViewById(R.id.house_area);
		house_type = (Spinner) findViewById(R.id.house_type);
		calc_type = (Spinner) findViewById(R.id.calc_type);
		
		house_type.setPrompt("请选择收入类型"); 
		ArrayAdapter<String> type_adapter = new ArrayAdapter<String>(SecondHandHouseCompute.this, android.R.layout.simple_spinner_item, house_type_str);
	    type_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
	    house_type.setAdapter(type_adapter);  

	    house_type.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

	           public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
	           {
	        	   house_type_result = arg2 + 1;
	        	   arg0.setVisibility(View.VISIBLE);
	           }

	           public void onNothingSelected(AdapterView<?> arg0)
	           {
	           }

	       });

	    
		ArrayAdapter<String> type_adapter2 = new ArrayAdapter<String>(SecondHandHouseCompute.this, android.R.layout.simple_spinner_item, calc_type_str);
	    type_adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
	    calc_type.setAdapter(type_adapter2);  

	    calc_type.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

	           public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
	           {
	        	   calc_type_result = arg2 + 1;
	        	   arg0.setVisibility(View.VISIBLE);
	           }

	           public void onNothingSelected(AdapterView<?> arg0)
	           {
	           }

	       });
		
		over_5_years = (CheckBox) findViewById(R.id.over_5_years);
		seller_only = (CheckBox) findViewById(R.id.seller_only);
		buyer_only = (CheckBox) findViewById(R.id.buyer_only);
		
		
		IntentFilter filter = new IntentFilter(RECI_COAST);
		BroadcastReceiver myrecive = new MyRecive();
		registerReceiver(myrecive, filter);
		
		
		Thread thread = new Thread() 
		{

			@Override
			public void run() 
			{
				Intent intent = new Intent(RECI_COAST);
				try {
					//获取服务器返回的信息
					String reslut = com.taxsystem.tool.HttUtil
							.getRequest(requestUrl);
					intent.putExtra("weatherinfo", reslut);
					//发送广播
					sendBroadcast(intent);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
			}

		};
	}

	private boolean isNoEmpty()
	{
		
		if(old_total_price.length()<1||total_price.length()<1||house_area.length()<1)
			return false;
		else
			return true;
			
	}
	
	public void check(View view)
	{
		if(isNoEmpty())
		{
		
		 int a=0,b=0,c=0;
		 if(over_5_years.isChecked())
			 a=1;
		 if(buyer_only.isChecked())
			 b=1;
		 if(seller_only.isChecked())
			 c=1;
		 String url = "http://mtaxtest.duapp.com/calculator/secondhandhousetax?calcType="+calc_type_result+"&houseType="+house_type_result+
				 "&area="+house_area.getText()+"&totalPrice="+total_price.getText()+"&oldTotalPrice="+old_total_price.getText()+"&boughtOver5Year="+a+
				 "&buyerFirstHouse="+b+"&sellerOnlyHouse="+c;	
			
		 Log.d("duck",url);
		 /*
		 String result = null;
		 try {
			Log.d("test", "befu"); 
			result = com.taxsystem.tool.HttUtil.getRequest(url);
			Log.d("test",result);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
		 
		 Intent intent = new Intent(this, SecondHandHouseResult.class);
		 intent.putExtra("result", result);
		 startActivity(intent);
		}
		else
		{
			Toast msg = Toast.makeText(SecondHandHouseCompute.this, "请把信息填写完整再查询", Toast.LENGTH_LONG);
			msg.show();
		}
	}
	
	// 定义BroadcastReceiver 接收返回的信息
    
	private class MyRecive extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				//获取JSONObject对象
				JSONObject jsonobject = new JSONObject(
						intent.getStringExtra("weatherinfo"));
				JSONObject jsoncity = new JSONObject(
						jsonobject.getString("weatherinfo"));
				/*
				show_result.setText("城市:" + jsoncity.getString("city") + "\t"
						+ "日期:" + jsoncity.getString("date_y") + "\n" + "今天天气:"
						+ jsoncity.getString("temp1") + "\t"
						+ jsoncity.getString("weather1")+"\t"+jsoncity.getString("wind1"));
						*/
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_mian, menu);
		menu.add(0, MY_NOTICE, 0, "我的通知");
		menu.add(0, NAV_SETTING, 0, "导航设置");
		menu.add(0, ACCOUNT_SETTING, 0, "账号设置");
		menu.add(0, INDIVIDUAL_SETTING, 0, "个性化设置");
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem mi)
	{
		
		
		//判断单击的是哪个菜单项，并针对性的作出响应。
		switch (mi.getItemId())
		{
			case android.R.id.home:
				this.finish();
				break;
			case MY_NOTICE:
				Log.d("hehe","heh");
				Toast toast1 = Toast.makeText(getApplicationContext(),
					     "MY_NOTICE", Toast.LENGTH_SHORT);
				toast1.show();
				break;
			case NAV_SETTING:
				Toast toast2 = Toast.makeText(getApplicationContext(),
					     "NAV_SETTING", Toast.LENGTH_SHORT);
				toast2.show();;
				break;
			case ACCOUNT_SETTING:
				Toast toast3 = Toast.makeText(getApplicationContext(),
					     "ACCOUNT_SETTING", Toast.LENGTH_SHORT);
				toast3.show();
				break;
			case INDIVIDUAL_SETTING:
				Toast toast4 = Toast.makeText(getApplicationContext(),
					     "INDIVIDUAL_SETTING", Toast.LENGTH_SHORT);
				toast4.show();
				break;

		}
		return true;
	}

}
